<?php

class Rnaasv_AuthAdapterDbTable extends Zend_Auth_Adapter_DbTable {

	protected $_tablaRoles;
	
	protected $_columnaIdRoles;
	
	protected $_roles;
	
	/**
	 * @return string
	 */
	public function getTablaRoles() {
		return $this->_tablaRoles;
	}
	
	/**
	 * @param Zend_Db_Table_Abstract $_roles
	 */
	public function setTablaRoles(Zend_Db_Table_Abstract $_roles) {
		$this->_tablaRoles = $_roles;
	}
	/**
	 * @return string
	 */
	public function getColumnaIdRoles() {
		return $this->_columnaIdRoles;
	}
	
	/**
	 * @param string|array $_columnaIdRoles
	 */
	public function setColumnaIdRoles($_columnaIdRoles) {
		$this->_columnaIdRoles = $_columnaIdRoles;
	}
	
	/**
	 * @see Zend_Auth_Adapter_DbTable::getResultRowObject()
	 *
	 * @param string|array $returnColumns
	 * @param string|array $omitColumns
	 * @return Rnaasv_User
	 */
	public function getResultRowObject($returnColumns, $omitColumns) {
		$resultRow = parent::getResultRowObject($returnColumns, $omitColumns);
        if (!$resultRow) {
            return $resultRow;
        }
        $omitColumns = (array) $omitColumns;
        if ($omitColumns !== null) {
	         if (	(is_array($omitColumns) && in_array('roles', $omitColumns)) || 
	         		($omitColumns === 'roles')) {
	            return $resultRow;
	         } 
        }
        if ($this->_roles === null) {
        	$where = $this->getColumnaIdRoles() . " = '" . $this->_identity . "'";
        	//var_dump($where);
        	$this->_roles = $this->getTablaRoles()->fetchAll($where)->toArray();
        }
        
        $rolesArray = array();
        for ($i = 0; $i < sizeof($this->_roles); $i++) {
        	$roleRow = $this->_roles[$i];
			$roleObject = new Rnaasv_CargoRole($roleRow);
			$rolesArray[] = $roleObject;
        }
        $resultRow->roles = $rolesArray;
        //var_dump($rolesArray);
        //die();
        return new Rnaasv_User($resultRow);
	}


}

?>
